package com.competition.web.admin.service.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import com.competition.web.admin.mapper.TableColumnInfoMapper;
import com.competition.web.admin.service.ITableColumnInfoService;
import com.basic.service.impl.BaseServiceImpl;
import com.common.util.StringUtil;
import com.competition.web.constant.Const;
import com.competition.web.constant.RedisConstant;
import com.dao.base.IBaseMapper;
import com.security.admin.security.util.JwtTokenUtil;
import com.competition.web.entity.LogContent;
import com.competition.web.entity.TableColumnComment;

@Service
public class TableColumnInfoServiceImpl extends BaseServiceImpl<TableColumnComment> implements ITableColumnInfoService {

	private JwtTokenUtil jwtTokenUtil;

	private RedisTemplate<String, Object> redisTemplate;
	

	@Autowired
	public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}

	final int CERTIFICATE_CLASSIFICATION = 1;

	public TableColumnInfoServiceImpl(TableColumnInfoMapper mapper) {
		super(mapper);
	}



	protected void insertPropteris(LogContent data) {

		long time =  (System.currentTimeMillis() / 1000);

		data.setCreateTime(time);
	}




	public TableColumnComment selectByPrimaryKey(int id) {

		String key = RedisConstant.getCertificateKey(id);

		ValueOperations<String, Object> valueOperations = this.redisTemplate.opsForValue();

		TableColumnComment certificateInfo = (TableColumnComment) valueOperations.get(key);

		if (null != certificateInfo) {
			return certificateInfo;
		}

		certificateInfo = super.selectByPrimaryKey(id);

		if (null == certificateInfo) {
			certificateInfo = new TableColumnComment();
		}

		valueOperations.set(key, certificateInfo, 5, TimeUnit.SECONDS);

		return certificateInfo;

	}
	
	public Map<String, String> getTableComment(String tableName) {
		
		String key = RedisConstant.getTableColumnInfoKey(tableName);

		ValueOperations<String, Object> valueOperations = this.redisTemplate.opsForValue();

		Map<String, String> tableComment = (Map<String, String>) valueOperations.get(key);

		if (null != tableComment) {
			return tableComment;
		}

		TableColumnInfoMapper columnInfoMapper = this.getBaseMapper();
		
		List<TableColumnComment> columnComments = columnInfoMapper.getTableColumnInfo(tableName, Const.DATA_BASE_NAME);

		if (null == columnComments) {
			tableComment = new HashMap<>();
		} else {
			
			TableColumnComment tableColumnComment;
			
			tableComment = new HashMap<>();
			
			for (Iterator<TableColumnComment> iterator = columnComments.iterator(); iterator.hasNext();) {
				tableColumnComment =  iterator.next();
				
				tableComment.put(StringUtil.camelize(tableColumnComment.getColumnName()), tableColumnComment.getColumnComment());
			}
		}

		valueOperations.set(key, tableComment, 1, TimeUnit.DAYS);

		return tableComment;
		
	}
	

//	@Override
//	public int insertParse(LogContent log, String tableName, Byte type) {
//		
//		TableComment comment = this.getTableComment(tableName);
//		
//		log.setComment(comment.getTableComment());
//		
//		log.setType(type);
//		
//		log.setTableName(tableName);
//
//		log.setId(0L);
//		
//		ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//		HttpServletRequest request = attributes.getRequest();
//		
//		String token = request.getHeader("Authorization");
//		
//		log.setAdminId(jwtTokenUtil.getId(token));
//		
//		log.setIp(RequestUtil.getRequestIp(request));
//		
//		return this.insert(log);
//	}

	public JwtTokenUtil getJwtTokenUtil() {
		return jwtTokenUtil;
	}

	public void setJwtTokenUtil(JwtTokenUtil jwtTokenUtil) {
		this.jwtTokenUtil = jwtTokenUtil;
	}

}